<template>
    <div id="tmpl">
        <!--提交评论结构-->
        <div id="postcomment">
            <h3>提交评论</h3>
            <p></p>
            <textarea placeholder="请输入评论内容..." v-model="postcontent"></textarea>
            <mt-button type="primary" size="large" @click="pastcomment">发表</mt-button>
        </div>
        <!--评论数据结构-->
        <div id="list">
            <h3>评论内容</h3>
            <p></p>
            <div v-for="(item,index) in list">
                <div class="title">
                    <span>第{{index+1}}楼</span>
                    <span>用户:{{item.user_name}}</span>
                    <span>发表时间:{{item.add_time | datefmt('YYYY-MM-DD HH:mm:ss')}}</span>
                </div>
                <ul class="mui-table-view">
                    <li class="mui-table-view-cell" v-text="item.content"></li>
                </ul>
            </div>
        </div>
        <!--更多按钮-->
        <div id="getmore">
            <mt-button type="danger" size="large" plain @click="getmore">加载更多</mt-button>
        </div>
    </div>
</template>
<script>
    import common from '../../kits/common.js';
    import {Toast} from 'mint-ui'
    export default{
        data(){
           return {
               postcontent:'',
               list:[],//评论数据
               pageindex:1
           }
        },
        props:['id'],
        created(){
            this.getcommentlist(this.pageindex);
        },
        methods:{
//            提交评论部分
            pastcomment(){
//                判断用户是否填写评论
                if(this.postcontent.trim().length <= 0){
                    Toast("请输入评论内容");
                    return
                }
                var url = common.apidomain+'/api/postcomment/'+this.id;
                this.$http.post(url,{content:this.postcontent},{emulateJSON:true}).then(function (res) {
                    Toast(res.body.message);

                    //追加评论到最顶部
                    this.list = [{
                        "user_name":"匿名用户",
                        "add_time":new Date,
                        "content":this.postcontent
                    }].concat(this.list);
                    this.postcontent = '';
                })
            },
            //加载更多
            getmore(){
                this.pageindex++;
                this.getcommentlist(this.pageindex);
            },
//            获取评论数据
            getcommentlist(pageindex){
                pageindex = pageindex || 1;
                var url = common.apidomain + '/api/getcomments/'+this.id+'?pageindex='+pageindex
                this.$http.get(url).then(function (res) {
                    if(res.body.status != 0){
                        Toast(res.body.message)
                    }
                    this.list = this.list.concat(res.body.message);
                })
            },

        }
    }

</script>
<style scoped>
    #postcomment{
        padding: 5px;
    }
    #postcomment p{
        height: 1px;
        width: 100%;
        border-bottom: 1px solid rgba(0,0,0,.3);
    }
    /*评论列表样式*/
    #list{
        padding: 5px;
    }
    .title{
        padding:5px;
        color: #6d6d72;
        font-size: 12px;
        background-color: #e2e2e2;
    }
    #getmore{
        padding: 5px;
    }
</style>